package com.geek.controller;


import com.geek.bean.BaseRespVo;
import com.geek.bean.PageData;
import com.geek.bean.data.CommentListData;
import com.geek.bean.vo.CommentCountVo;
import com.geek.bean.vo.CommentListVo;
import com.geek.bean.vo.CommentPostVo;
import com.geek.bean.vo.CommentVo;
import com.geek.model.MarketComment;
import com.geek.service.CommentService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("wx/comment")
public class WxCommentController {
    @Autowired
    CommentService commentService;

    @GetMapping("list")
    public BaseRespVo list(CommentListVo commentListVo) {
        List<CommentListData> data= commentService.list(commentListVo.getType(),
                commentListVo.getShowType(),
                commentListVo.getValueId(),
                commentListVo.getPage(),
                commentListVo.getLimit());
        PageData list = PageData.list(data);
        return BaseRespVo.ok(list);
    }
    @GetMapping("count")
    public BaseRespVo count(CommentCountVo commentCountVo){
        CommentVo count = commentService.count(commentCountVo.getType(),
                commentCountVo.getValueId());
        return BaseRespVo.ok(count);
    }
    @PostMapping("post")
    public BaseRespVo post(@RequestBody CommentPostVo comment){
        Subject subject = SecurityUtils.getSubject();
        if (subject.isAuthenticated()) {
            Integer userId = (Integer) subject.getPrincipal();
            MarketComment postCommentVo= commentService.post(userId, comment.getValueId(),
                    comment.getContent(),
                    comment.getStar(),
                    comment.getHasPicture(),
                    comment.getType(),
                    comment.getPicUrls());
            return BaseRespVo.ok(postCommentVo);
        }else {
            return BaseRespVo.unAuthc();
        }
    }
}
